//index.js
//获取应用实例
var app = getApp();
//console.log(app.globalData);
Page({
  data: {
    userInfo: {},
    imageList: [],
    imageKeys: [],
    topicType: 2,
    type: '',
    groupData: {},
    topicData: {},
    videoSrc: '',
    videoKey: '',
    topic_id: 0,
    chooseVideoText: '选择视频',
    status: 0,


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options);
    //options.topic_id = 83556;
    this.setData({ startYear: app.date(), startdate: app.date() })
    //this.setData({ enddate: app.date(7) })
    this.setData({ window: app.globalData.window, id: options.id,type: options.type })
    if (this.data.id && this.data.id > 0) {
      this.getGroup();
    }
    app.getUserInfo((userInfo) => {
      this.data.userInfo = userInfo;
    })

  },



  //
  getGroup: function () {
    var that = this;
    var id = this.data.id;
    app.api("sns/group/find", {
      id: id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({ groupData: res.data, type: res.data.type });
      wx.setNavigationBarTitle({
        title: res.data.content.issuePageTitle
      });
    })
  },
  formSubmit: function (e) {
    var formId = e.detail.formId;
    var user_info = app.globalData.userInfo;
    var postData = e.detail.value;
    if (this.data.loading == 1) {
      return;
    }

    if (postData.group_name == "") {
      app.alert('请先设置圈子名称');
      return false;
    }


    var groupData = this.data.groupData;

    this.setData({ 'loading': 1 });
    this.setData({ 'disabled': 1 });
    var _this = this;
    var title = postData.title;

    //提交回答
    app.api("sns/group/add", {  
      formId: formId,
      group_name: postData.group_name,
      type: this.data.type,
      status: 1,
      orderBy:999,
      id: this.data.id?this.data.id:0,
     
      method: 'POST',
    }, function (res) {
      _this.setData({ 'loading': 0 });
      _this.setData({ 'disabled': 0 });
      var title = "";
      if (res.code == 200) {
        // wx.showToast({
        //   title: '发布成功',
        //   icon: 'success',
        //   duration: 2000
        // });

        var topic_id = res.data.id;
        _this.suc();
        // if (!_this.data.signIn) {//心愿单
        //   _this.join_topic(topic_id);
        // } else {
        //   _this.suc();
        // }
        // 
        //  
        // _this.navigateBack();
      } else {
        app.alert(res.msg);
      }
    })
  },


  suc: function () {
    var _this = this;
    wx.showToast({
      title: '发布成功',
      icon: 'success',
      duration: 2000
    });
    app.setPageData("group/index", { reload: 1 });
    _this.navigateBack();
  },
  //返回 
  navigateBack: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },


})
